
import pandas as pd
from ths.codeList import Tonghuashun
import baostock as bs
import talib
from util.GetBaostockDatByDataFrame import GetBaostockDataFrame

if __name__ == "__main__":
    try:

        lg = bs.login(user_id='anonymous', password='123456')
        t = Tonghuashun("test",
                        "__utma=156575163.1354766489.1663510434.1663510434.1663510434.1; __utmz=156575163.1663510434.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); log=; Hm_lvt_78c58f01938e4d85eaf619eae71b4ed1=1663469443,1663508725,1663771017,1665052333; user=MDq35kh0Njo6Tm9uZTo1MDA6MzI4ODA3NTY4OjcsMTExMTExMTExMTEsNDA7NDQsMTEsNDA7NiwxLDQwOzUsMSw0MDsxLDEwMSw0MDsyLDEsNDA7MywxLDQwOzUsMSw0MDs4LDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAxLDQwOzEwMiwxLDQwOjI0Ojo6MzE4ODA3NTY4OjE2NjUwNTIzNjM6OjoxNDUyODE0ODYwOjYwNDgwMDowOjE4ZjE4ZmEwNWYxNWZhMTlkY2NiOTAxMmMxYjIwZDAxNjpkZWZhdWx0XzQ6MQ%3D%3D; userid=318807568; u_name=%B7%E6Ht6; escapename=%25u950bHt6; ticket=668ece4d412c33302fcd27468ff51d69; user_status=0; utk=a19a2f1d068233c8a86bd04459935afd; Hm_lvt_da7579fd91e2c6fa5aeb9d1620a9b333=1663471001,1663508726,1663771017,1665052409; Hm_lpvt_da7579fd91e2c6fa5aeb9d1620a9b333=1665052409; Hm_lpvt_78c58f01938e4d85eaf619eae71b4ed1=1665052409; v=A09kfeMRcHiAmXQE_3VK5uFs3uhcdKOqPcqnimFc677FMGGWaUQz5k2YN9Ny")
        data = t.get_stocks()
        for index, row in data.iterrows():
            code = row['code']
            #print(code)
            t.modify_stock(code, 'del', None)

        rs = bs.query_all_stock(day='2022-4-7')
        i=0
        df1 = []
        df =pd.DataFrame(columns=['high', 'open', 'close', 'low','ma25','bias_24','bizhi'])
        while (rs.error_code == '0') & rs.next():
            try:
                i=i+1
                code = rs.get_row_data()[0]
                if ("bj" in code) == False:

                    data = GetBaostockDataFrame().getData(bs,code, 'd',100,50, False)
                    data['rsi6'] = talib.RSI(data.close, timeperiod=6)
                    # data['rsi14'] = talib.RSI(data.close, timeperiod=14)
                    data['ma25'] = data['close'].rolling(window=24).mean()
                    data['bias_24'] = (data['close'] - data['close'].rolling(24, min_periods=1).mean()) / data['close'].rolling(24, min_periods=1).mean() * 100
                    data['bias_24'] = round(data['bias_24'], 2)
                    data['bias_6'] = (data['close'] - data['close'].rolling(6, min_periods=1).mean()) / data['close'].rolling(6, min_periods=1).mean() * 100
                    data['bias_6'] = round(data['bias_6'], 2)
                    data['bias_12'] = (data['close'] - data['close'].rolling(12, min_periods=1).mean()) / data['close'].rolling(12, min_periods=1).mean() * 100
                    data['bias_12'] = round(data['bias_12'], 2)
                    data['bizhi'] = (data['ma25'] - data['close']) / data['ma25']

                    data['bias'] = data['bias_6'] - data['bias_12']

                    #print(data)

                    #print(data)\
                    d4 = data.iloc[-4]
                    d3 = data.iloc[-3]
                    d2 = data.iloc[-2]
                    d1 = data.iloc[-1]

                    d3_bias = d3.bias_6 - d3.bias_12
                    d2_bias = d2.bias_6 - d2.bias_12
                    d1_bias = d1.bias_6 - d1.bias_12
                    if  d1.rsi6 > d2.rsi6 and d1.bias_24 > d2.bias_24 and d1.low < d2.low  and d3.bias<d2.bias and d2.bias <d1.bias   and d1.bias>0 and  d2.bias>0 and  d3.bias>0:
                        print(d1.code,i)
                        df1.append(d1.values)
            except Exception as err:
                1+1
                #print(err)

        df_init = pd.DataFrame(df1, columns=data.columns)
        df_init = df_init.sort_values(by='bias', ascending=True)
        df_init =df_init[0:100]

        df_init = df_init.sort_values(by='bizhi', ascending=False)
        df_init = df_init[0:10]

        print(df_init)

        for index, row in df_init.iterrows():
            code = (row.code).split('.', 1)
            print(code)
            t.modify_stock(code, 'add', pos=None)

        print("运行完成!")



















        #t = Tonghuashun("test", "user_status=0; log=; Hm_lvt_f79b64788a4e377c608617fba4c736e2=1663469443; Hm_lvt_78c58f01938e4d85eaf619eae71b4ed1=1663426762,1663469443; cmsad_156_1=0; cmsad_152_1=0; cmsad_150_0=0; cmsad_160_1=0; user=MDq35kh0Njo6Tm9uZTo1MDA6MzI4ODA3NTY4OjcsMTExMTExMTExMTEsNDA7NDQsMTEsNDA7NiwxLDQwOzUsMSw0MDsxLDEwMSw0MDsyLDEsNDA7MywxLDQwOzUsMSw0MDs4LDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAxLDQwOzEwMiwxLDQwOjI0Ojo6MzE4ODA3NTY4OjE2NjM0Njk0Njc6OjoxNDUyODE0ODYwOjYwNDgwMDowOjE0NDc4OTcyZjg2NGE3ZWFiNDE2NzZkODJlNjYwZTIwNzpkZWZhdWx0XzQ6MQ%3D%3D; userid=318807568; u_name=%B7%E6Ht6; escapename=%25u950bHt6; ticket=2c44535375476af83bda4c7f04018e5c; utk=54cdad4f3d3fe57b3e86e436ba6a3c78; Hm_lpvt_78c58f01938e4d85eaf619eae71b4ed1=1663469470; cmsad_152_0=0; Hm_lpvt_f79b64788a4e377c608617fba4c736e2=1663469470; v=A5a90mIS6UfZI93jt9DZ1De650edN9pULHoO1QD_h3kUyDj5aMcqgfwLXurT")
        #data = t.get_stocks()
        #for index, row in data.iterrows():
        #    code = row['code']
            #t.modify_stock(code, 'del', pos='1')

        #添加
        #boo = t.modify_stock('834475', 'add', pos='1')
        #if boo :
        #    print('修改成功')


        #print(data)

    except Exception as err:
        print("Finished with error: ", err)